/*!
https://gitee.com/uqbn/labs/raw/master/unit1/main.c
*/
#include "main.h"

#define KEY0 P3_0
#define KEY1 P3_1
#define KEY2 P3_2
#define KEY3 P3_3
#define KEY4 P3_4
#define KEY5 P3_5
#define KEY6 P3_6
#define KEY7 P3_7

#define KEY0_MSK (1U << 0U)
#define KEY1_MSK (1U << 1U)
#define KEY2_MSK (1U << 2U)
#define KEY3_MSK (1U << 3U)
#define KEY4_MSK (1U << 4U)
#define KEY5_MSK (1U << 5U)
#define KEY6_MSK (1U << 6U)
#define KEY7_MSK (1U << 7U)

#define CHECK_KEY(_key, _msk)        \
    do                               \
    {                                \
        if (!_key)                   \
        {                            \
            delay(10U);              \
            if (!_key)               \
            {                        \
                CLEAR_BIT(P1, _msk); \
            }                        \
            else                     \
            {                        \
                SET_BIT(P1, _msk);   \
            }                        \
        }                            \
        else                         \
        {                            \
            SET_BIT(P1, _msk);       \
        }                            \
    } while (0)

static void delay(uint16_t xms);

void delay(uint16_t xms)
{
    uint16_t i, j;
    for (i = xms; i; --i)
    {
        for (j = 112U; j; --j)
        {
        }
    }
}

int main(void)
{
    for (;;)
    {
        CHECK_KEY(KEY0, KEY0_MSK);
        CHECK_KEY(KEY1, KEY1_MSK);
        CHECK_KEY(KEY2, KEY2_MSK);
        CHECK_KEY(KEY3, KEY3_MSK);
        CHECK_KEY(KEY4, KEY4_MSK);
        CHECK_KEY(KEY5, KEY5_MSK);
        CHECK_KEY(KEY6, KEY6_MSK);
        CHECK_KEY(KEY7, KEY7_MSK);
    }
}

/* END OF FILE */
